package com.steven.ourcontest.chat.view

import android.os.Bundle
import android.view.View
import androidx.core.view.isVisible
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer
import com.steven.ourcontest.R
import com.steven.ourcontest.app.AppRouter
import com.steven.ourcontest.base.BaseFragment
import com.steven.ourcontest.chat.adapter.ApplyViewBinder
import com.steven.ourcontest.chat.adapter.MessageViewBinder
import com.steven.ourcontest.chat.bean.ApplyFriend
import com.steven.ourcontest.chat.bean.ChatReceiveMessageBean
import com.steven.ourcontest.chat.bean.WSApplyFriendResponse
import com.steven.ourcontest.chat.bean.WSChatOnlineMessageResponse
import com.steven.ourcontest.chat.bean.WSMessageResponse
import com.steven.ourcontest.chat.helper.MessageHelper
import com.steven.ourcontest.chat.viewmodel.FriendViewModel
import com.steven.ourcontest.utils.RxBus
import com.steven.ourcontest.utils.plusAssign
import kotlinx.android.synthetic.main.fragment_message.*
import me.drakeet.multitype.Items
import me.drakeet.multitype.MultiTypeAdapter

class MessageFragment : BaseFragment() {

    private val adapter by lazy {
        MultiTypeAdapter()
    }
    private val item by lazy {
        Items()
    }
    private val friendViewModel: FriendViewModel by activityViewModels()
    override fun getViewId(): Int? = R.layout.fragment_message

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
        initListener()
    }

    private fun initView() {
        recycler_container?.adapter = adapter
        adapter.items = item
        adapter.register(ChatReceiveMessageBean::class.java, MessageViewBinder(context) { uid, name ->
            friendViewModel.updateMessageStatus(uid){
                MessageHelper.messageUnReadMap.remove(uid)
                notifyDataSetChanged()
            }
            context?.let { it1 -> AppRouter.startChatActivity(it1, uid, name) }
        })
        adapter.register(ApplyFriend::class.java, ApplyViewBinder(context) { uid, agree ->
            friendViewModel.updateFriendShip(uid, agree)
        })
        MessageHelper.messageUnReadMap.forEach {
            item.add(ChatReceiveMessageBean(it.value.size, it.value.last()))
        }
        MessageHelper.applyUnReadMap.forEach {
            item.add(it.value)
        }
        adapter.notifyDataSetChanged()
    }

    private fun initListener() {
        disposables += RxBus.getInstance()
            .toObservable(viewLifecycleOwner, WSMessageResponse::class.java).subscribe {
                notifyDataSetChanged()
            }
        disposables += RxBus.getInstance()
            .toObservable(viewLifecycleOwner, WSApplyFriendResponse::class.java).subscribe {
                notifyDataSetChanged()
            }
        friendViewModel.updateFriendShip.observe(viewLifecycleOwner, Observer {
            MessageHelper.applyUnReadMap.remove(it)
            notifyDataSetChanged()
        })
    }

    private fun notifyDataSetChanged() {
        item.clear()
        var hasItem = false
        MessageHelper.messageUnReadMap.forEach {
            hasItem = true
            item.add(ChatReceiveMessageBean(it.value.size, it.value.last()))
            println("消息添加")
        }
        MessageHelper.applyUnReadMap.forEach {
            hasItem = true
            item.add(it.value)
            println("申请好友添加")
        }
        adapter.notifyDataSetChanged()
        recycler_container?.isVisible = hasItem
        tv_message_none?.isVisible = !hasItem
    }

    override fun onResume() {
        super.onResume()
        tv_message_none?.isVisible = item.isEmpty()
    }
}